﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Core;
using Sisocana.Seguridad;


namespace Sisocana.Controllers
{
    public class VideoController : Controller
    {
        private IFuenteService _fuenteService;

        public VideoController()
            : this(new FuenteService())
        {
        }

        public VideoController(IFuenteService fuenteService)
        {
            _fuenteService = fuenteService;
        }

        //
        // GET: /Video/
        [Authorize]
        public ActionResult Index()
        {
            IList<VideoModel> videos = _fuenteService.GetAllVideo();
            return View(videos);
        }

        //
        // GET: /Video/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Video/Create
        [Authorize]
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "logo")]VideoModel vid)
        {
            try
            {
                HttpPostedFileBase logo = Request.Files["logo"];


                VideoModel vidS = new VideoModel();
                vidS.nombre = vid.nombre;
                vidS.pagWeb = vid.pagWeb;
                vidS.descripcion = vid.descripcion;


                if (logo != null && logo.ContentLength > 0)
                {
                    vidS.logoNomb = logo.FileName;
                    vidS.logoExt = logo.ContentType;

                    byte[] img = new byte[logo.InputStream.Length];
                    logo.InputStream.Read(img, 0, img.Length);
                    vidS.logo = img;
                }

                if (_fuenteService.addVideo(vidS) == 0)
                    return RedirectToAction("Index");
                else
                    return View();
            }
            catch (Exception e)
            {
                return View();
            }
        }

        //
        // GET: /Video/Edit/5
        [AdminGralAuthorize]
        public ActionResult Edit(string id)
        {
            VideoModel vi = _fuenteService.getSigleVideo(a => a.nombre.Trim() == id);
            return View(vi);
        }

        //
        // POST: /Video/Edit/5
        [AdminGralAuthorize]
        [HttpPost]
        public ActionResult Edit([Bind(Exclude = "logo")]VideoModel vi)
        {
            try
            {
                HttpPostedFileBase logo = Request.Files["logo"];

                if (logo != null && logo.ContentLength > 0)
                {
                    vi.logoNomb = logo.FileName;
                    vi.logoExt = logo.ContentType;

                    byte[] img = new byte[logo.InputStream.Length];
                    logo.InputStream.Read(img, 0, img.Length);
                    vi.logo = img;
                }

                if (_fuenteService.updateVideo(vi) == 0)
                    return RedirectToAction("Index");
                else
                    return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Video/Delete/5
        [AdminGralAuthorize]
        public ActionResult Delete(string id)
        {
            VideoModel vi = _fuenteService.getSigleVideo(a => a.nombre.Trim() == id);
            return View(vi);
        }

        //
        // POST: /Video/Delete/5
        [AdminGralAuthorize]
        [HttpPost]
        public ActionResult Delete([Bind(Exclude = "logo")]VideoModel vi)
        {
            try
            {
                int res=_fuenteService.deleteVideo(vi);
                if ( res== 0)
                    return RedirectToAction("Index");
                else
                {
                    return RedirectToAction("MessageID","Error",new{ errorid=res} );
                }
            }
            catch
            {
                return RedirectToAction("MessageID", "Error", new { errorid = -110 });
            }
        }


        public void Imagen(string id)
        {
            VideoModel rs = _fuenteService.getSigleVideo(a => a.nombre.Trim() == id);
            Response.ContentType = rs.logoExt;
            Response.BinaryWrite(rs.logo);
        }

    }
}